""" 
测试 pickle 序列化 与 反序列化
"""
import os
import pickle
from get_python_obj_size import get_python_obj_size

def pickle_save(file, obj):
    with open(file, "wb") as f:
        pickle.dump(obj, f)

def pickle_load(file):
    with open(file, "rb") as f:
        return pickle.load(f)

# 列表list 序列化
def test_pickle_list():
    a = [1, 2, 3, 4, 5, 6, 8, "111", "222", "333"]
    pickle_save("a.pkl", a)
    a_load = pickle_load("a.pkl")
    print(a_load)
    # '[1, 2, 3, 4, 5, 6, 8, '111', '222', '333']'

    pkl_size = os.path.getsize("a.pkl")
    mem_size = get_python_obj_size(a)
    print(f"mem_size:{mem_size}, pkl_size: {pkl_size}")

# 字典dict 序列化
def test_pickle_dict():
    a = {
        "name": "lilisi",
        "age": 18,
        "address": "GD-SZ",
        "hobbies": ["swimming", "reading", "running"],
    }
    pickle_save("b.pkl", a)
    a_load = pickle_load("b.pkl")
    print(a_load)

    pkl_size = os.path.getsize("b.pkl")
    mem_size = get_python_obj_size(a)
    print(f"mem_size:{mem_size}, pkl_size: {pkl_size}")
    
test_pickle_list()
test_pickle_dict()

""" 
[1, 2, 3, 4, 5, 6, 8, '111', '222', '333']
mem_size:488, pkl_size: 48

{'name': 'lilisi', 'age': 18, 'address': 'GD-SZ', 'hobbies': ['swimming', 'reading', 'running']}
mem_size:843, pkl_size: 103

"""